import 'package:demo/event/event_setting.dart';
import 'package:demo/views/base/base_controller.dart';
import 'package:demo/tools/tools_enum.dart';
import 'package:demo/tools/tools_sqlite.dart';
import 'package:demo/component/component_contact.dart';

class GroupIndexController extends BaseController {
  // 联系人列表
  List<ContactModel> dataList = [];
  // 刷新
  _onRefresh() async {
    // 执行
    List<ChatGroup> groupList = await ToolsSqlite().group.getList();
    dataList.clear();
    // 转换
    for (var group in groupList) {
      dataList.add(
        ContactModel(
          userId: group.groupId,
          nickname: group.groupName,
          portrait: group.portrait,
          extend: '群ID：${group.groupNo}',
        ),
      );
    }
    update();
  }

  @override
  void onInit() {
    super.onInit();
    // 刷新群聊
    _onRefresh();
    // 监听群聊
    subscription = EventSetting().event.stream.listen((model) {
      if (SettingType.group == model.setting) {
        // 刷新群聊
        _onRefresh();
      }
    });
  }
}
